package com.kancy.framework.job.starter.job;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;

/**
 * 抽象的SimpleJob
 * @author kancy
 */
public abstract class AbstractSimpleJob implements SimpleJob {

    private ShardingContextHolder shardingContextHolder = new ShardingContextHolder();

    /**
     * 执行作业.
     *
     * @param shardingContext 分片上下文
     */
    @Override
    public final void execute(ShardingContext shardingContext) {
        try {
            shardingContextHolder.set(shardingContext);
            /**
             * 实际开发中，有了任务总片数和当前分片项，就可以对任务进行分片执行了
             * 比如 SELECT * FROM table WHERE status = 0 AND MOD(id, shardingTotalCount) = shardingItem limit 1000
             */
            execute();
        } finally {
            shardingContextHolder.remove();
        }
    }

    /**
     * 执行作业.
     */
    protected abstract void execute();

}
